// locutor
// (c) 2009, Normen Müller, normen.mueller@gmail.com  
// $Id: RegistrySpecDelete.scala 3 2009-11-06 19:37:06Z normen.mueller $
package locutor.spec.registry

import org.specs._

import java.io.File
import scaup.Views._
import scaup.io.FileType._
import locutor.registry._
import locutor.spec.Util._

object RegistrySpecDelete extends Specification("Delete") {
  def throwWarnings = addToSusVerb("throw warnings due to")
  "Removing" should throwWarnings {
    "working copy is not registered" in {
      ioRemove("wca")
      ioMkDir("wca")
      newRegistry().delete(WCA) must throwA[RegistryWarning]
    }
  }  


  def unregister = addToSusVerb("unregister")
  "Removing" should unregister {
    "a working copy" in {
      setupWC("wca")
      
      val r = newRegistry()      
      r.insert(WCA)
      r.serialize()
      
      scala.xml.XML.loadFile(REG) must ==/(
        (<registry version="0.1" xmlns="http://locutor.kwarc.info">
           <wc xml:id={WCA.path.toMD5} url={URLA} root={WCA.path}/>
         </registry>))
      
      r.delete(WCA)
      r.serialize()
      
      scala.xml.XML.loadFile(REG) must ==/((<registry version="0.1" xmlns="http://locutor.kwarc.info"/>))
    }
    "a working copy /and/ reset dependents" in {
      setupWC("wca")
      setupWC("wcb")
      
      val r = newRegistry()
      r.insert(WCA)
      r.insert(WCB)
      
      r.sanitize(WCB)
      r.delete(WCA)
      r.serialize()
      
      scala.xml.XML.loadFile(REG) must ==/(
        (<registry version="0.1" xmlns="http://locutor.kwarc.info">
           <wc xml:id={WCB.path.toMD5} url={URLB} root={WCB.path}>
             <external xml:id={(WCB/"a").path.toMD5} own={WCB.path} rev="-1" url={URLA} tgt="a"/>
           </wc>
         </registry>))
      (WCB/"a").t must_== DIRECTORY
    }
    "a transformed working copy (1st example)" in {
      setupWC("wca")
      setupWC("wcb")
      
      val r = newRegistry()
      r.insert(WCA)
      r.insert(WCB)
      
      r.sanitize(WCB)
      r.delete(WCB)
      r.serialize()
      
      scala.xml.XML.loadFile(REG) must ==/(
        (<registry version="0.1" xmlns="http://locutor.kwarc.info">
           <wc xml:id={WCA.path.toMD5} url={URLA} root={WCA.path}/>
         </registry>))
      (WCB/"a").t must_== DIRECTORY
    }
    "a transformed working copy (2nd example)" in {
      setupWC("wca")
      setupWC("wcb")
      setupWC("wcc")
      
      val r = newRegistry()
      r.insert(WCA)
      r.insert(WCB)
      r.insert(WCC)
      r.sanitize(WCB)
      r.sanitize(WCC)
      r.delete(WCC)
      r.serialize()
      
      scala.xml.XML.loadFile(REG) must ==/(
        (<registry version="0.1" xmlns="http://locutor.kwarc.info">
           <wc xml:id={WCA.path.toMD5} url={URLA} root={WCA.path} />
           <wc xml:id={WCB.path.toMD5} url={URLB} root={WCB.path}>
             <external xml:id={(WCB/"a").path.toMD5} tgt="a" url={URLA} own={WCB.path} rev="-1" xref={"#"+WCA.path.toMD5}/>             
           </wc>
         </registry>))
      (WCB/"a").t must_== SYMLINK
      (WCC/"a").t must_== DIRECTORY
      (WCB/"a").lnktgt must_== Some(WCA.path)
    }
    "a transformed working copy (3rd example)" in {
      setupWC("wca")
      setupWC("wcf")
      setupWC("wcg")
      svnUpdate("wcf")
      svnUpdate("wcg")

      val r = newRegistry()
      r.insert(WCF)
      r.insert(WCG)
      r.sanitize(WCG)
      r.insert(WCA)
      r.sanitize(WCG)
      r.delete(WCG)
      r.serialize()
      
      scala.xml.XML.loadFile(REG) must ==/(
        (<registry version="0.1" xmlns="http://locutor.kwarc.info">
           <wc xml:id={WCA.path.toMD5} url={URLA} root={WCA.path}/>
           <wc xml:id={WCF.path.toMD5} url={URLF} root={WCF.path}>
             <external xml:id={(WCF/"a").path.toMD5} own={WCF.path} rev="-1" url={(URLA.toURI/"a").toString} tgt="a"/>
             <external xml:id={(WCF/"b").path.toMD5} own={WCF.path} rev="-1" url={(URLA.toURI/"b").toString} tgt="b"/>
           </wc>
         </registry>))
      (WCG/"a").t must_== DIRECTORY
    }
    "a transformed working copy (4th example)" in {
      setupWC("wca")
      setupWC("wcf")
      setupWC("wcg")
      svnUpdate("wcf")
      svnUpdate("wcg")

      val r = newRegistry()
      r.insert(WCA)
      r.insert(WCF)
      r.insert(WCG)
      r.sanitize(WCG)
      r.sanitize(WCF)
      r.delete(WCG)
      r.serialize()
      
      scala.xml.XML.loadFile(REG) must ==/(
        (<registry version="0.1" xmlns="http://locutor.kwarc.info">
           <wc xml:id={WCA.path.toMD5} url={URLA} root={WCA.path}/>
           <wc xml:id={WCF.path.toMD5} url={URLF} root={WCF.path}>
             <external xml:id={(WCF/"a").path.toMD5} own={WCF.path} rev="-1" url={(URLA.toURI/"a").toString} tgt="a" xref={"#"+WCA.path.toMD5}/>
             <external xml:id={(WCF/"b").path.toMD5} own={WCF.path} rev="-1" url={(URLA.toURI/"b").toString} tgt="b" xref={"#"+WCA.path.toMD5}/>
           </wc>
         </registry>))
      (WCG/"a").t must_== DIRECTORY
      (WCF/"a").t must_== SYMLINK
      (WCF/"b").t must_== SYMLINK
      (WCF/"a").lnktgt must_== Some((WCA/"a").path)
      (WCF/"b").lnktgt must_== Some((WCA/"b").path)
    }
    "a transformed working copy (5th example)" in {
      setupWC("wca")
      setupWC("wcb")
      setupWC("wce")
      svnUpdate("wcb")
      
      var r = newRegistry()
      r.insert(WCB)
      r.insert(WCE)
      r.sanitize(WCE)
      r.insert(WCA)
      
      ioRemove("wcb")
      
      r.sanitize(WCE)
      r.serialize()
      
      scala.xml.XML.loadFile(REG) must ==/(
        (<registry version="0.1" xmlns="http://locutor.kwarc.info">
           <wc xml:id={WCA.path.toMD5} url={URLA} root={WCA.path}/>
           <wc xml:id={WCB.path.toMD5} url={URLB} root={WCB.path}>
             <external xml:id={(WCB/"a").path.toMD5} own={WCB.path} rev="-1" url={URLA} tgt="a"/>
           </wc>
           <wc xml:id={WCE.path.toMD5} url={URLE} root={WCE.path}>
             <external xml:id={(WCE/"a").path.toMD5} own={WCE.path} rev="-1" url={URLA} tgt="a" xref={"#"+WCA.path.toMD5}/>
           </wc>
         </registry>))
      (WCE/"a").t must_== SYMLINK
      (WCE/"a").lnktgt must_== Some(WCA.path)
      
      r.delete(WCB)
      r.sanitize(WCE)
      r.serialize()
      
      scala.xml.XML.loadFile(REG) must ==/(
        (<registry version="0.1" xmlns="http://locutor.kwarc.info">
           <wc xml:id={WCA.path.toMD5} url={URLA} root={WCA.path}/>
           <wc xml:id={WCE.path.toMD5} url={URLE} root={WCE.path}>
             <external xml:id={(WCE/"a").path.toMD5} own={WCE.path} rev="-1" url={URLA} tgt="a" xref={"#"+WCA.path.toMD5}/>
           </wc>
         </registry>))
      (WCE/"a").t must_== SYMLINK
      (WCE/"a").lnktgt must_== Some(WCA.path)
    }
    "a transformed working copy /and/ reset dependents (1st example)" in {
      setupWC("wca")
      setupWC("wcb")
      setupWC("wcc")
      
      val r = newRegistry()
      r.insert(WCA)
      r.insert(WCB)
      r.insert(WCC)
      r.sanitize(WCB)
      r.sanitize(WCC)
      r.delete(WCB)
      r.serialize()
      
      scala.xml.XML.loadFile(REG) must ==/(
        (<registry version="0.1" xmlns="http://locutor.kwarc.info">
           <wc xml:id={WCA.path.toMD5} url={URLA} root={WCA.path} />
           <wc xml:id={WCC.path.toMD5} url={URLC} root={WCC.path}>
             <external xml:id={(WCC/"a").path.toMD5} own={WCC.path} rev="-1" url={URLB} tgt="a">
               <external xml:id={(WCC/"a/a").path.toMD5} tgt="a" url={URLA} own={(WCC/"a").path} rev="-1" xref={"#"+WCA.path.toMD5}/>
             </external>
           </wc>
         </registry>))
      (WCB/"a").t must_== DIRECTORY
      (WCC/"a").t must_== DIRECTORY
      (WCC/"a/a").t must_== SYMLINK
      (WCC/"a/a").lnktgt must_== Some(WCA.path)
    }
    "a transformed working copy /and/ reset dependents (2nd example)" in {
      setupWC("wca")
      setupWC("wcb")
      setupWC("wcd")
      svnUpdate("wcb")
      
      val r = newRegistry()
      r.insert(WCB)
      r.insert(WCD)
      r.sanitize(WCD)
      r.delete(WCD)
      r.serialize()

      scala.xml.XML.loadFile(REG) must ==/(
        (<registry version="0.1" xmlns="http://locutor.kwarc.info">
           <wc xml:id={WCB.path.toMD5} url={URLB} root={WCB.path}>
             <external xml:id={(WCB/"a").path.toMD5} own={WCB.path} rev="-1" url={URLA} tgt="a"/>
           </wc>
         </registry>))
      (WCD/"a").t must_== DIRECTORY
    }
    "a transformed working copy /and/ reset dependents (3rd example)" in {
      setupWC("wca")
      setupWC("wcb")
      setupWC("wcd")
      svnUpdate("wcb")
      
      val r = newRegistry()
      r.insert(WCB)
      r.insert(WCD)
      r.sanitize(WCD)
      
      ioRemove("wcd")
      
      r.delete(WCD)
      r.serialize()

      scala.xml.XML.loadFile(REG) must ==/(
        (<registry version="0.1" xmlns="http://locutor.kwarc.info">
           <wc xml:id={WCB.path.toMD5} url={URLB} root={WCB.path}>
             <external xml:id={(WCB/"a").path.toMD5} own={WCB.path} rev="-1" url={URLA} tgt="a"/>
           </wc>
         </registry>))      
    }
    "a transformed working copy /and/ resolve dependents (1st example)" in {
      setupWC("wca")
      setupWC("wcb")
      setupWC("wcc")
      svnUpdate("wcc")
      
      val r = newRegistry()
      r.insert(WCA)
      r.insert(WCB)
      r.insert(WCC)
      r.sanitize(WCC)
      r.delete(WCB)
      r.serialize()
      
      scala.xml.XML.loadFile(REG) must ==/(
        (<registry version="0.1" xmlns="http://locutor.kwarc.info">
           <wc xml:id={WCA.path.toMD5} url={URLA} root={WCA.path} />
           <wc xml:id={WCC.path.toMD5} url={URLC} root={WCC.path}>
             <external xml:id={(WCC/"a").path.toMD5} own={WCC.path} rev="-1" url={URLB} tgt="a">
               <external xml:id={(WCC/"a/a").path.toMD5} tgt="a" url={URLA} own={(WCC/"a").path} rev="-1" xref={"#"+WCA.path.toMD5}/>  
             </external>
           </wc>
         </registry>))
      (WCB/"a").t must_== NONE
      (WCC/"a").t must_== DIRECTORY
      (WCC/"a/a").t must_== SYMLINK
      (WCC/"a/a").lnktgt must_== Some(WCA.path)
    }
    "a transformed working copy /and/ resolve dependents (2nd example)" in {
      setupWC("wca")
      setupWC("wcb")
      setupWC("wce")
      svnUpdate("wcb")
      
      val r = newRegistry()
      r.insert(WCB)
      r.insert(WCE)
      r.sanitize(WCE)
      r.insert(WCA)
      r.delete(WCB)
      r.serialize()
      
      scala.xml.XML.loadFile(REG) must ==/(
        (<registry version="0.1" xmlns="http://locutor.kwarc.info">
           <wc xml:id={WCA.path.toMD5} url={URLA} root={WCA.path}/>
           <wc xml:id={WCE.path.toMD5} url={URLE} root={WCE.path}>
             <external xml:id={(WCE/"a").path.toMD5} own={WCE.path} rev="-1" url={URLA} tgt="a" xref={"#"+WCA.path.toMD5}/>
           </wc>
         </registry>))
      (WCE/"a").t must_== SYMLINK
      (WCE/"a").lnktgt must_== Some(WCA.path)
    }
  }
}
